<template>
  <!-- <div>尾程时效分析</div> -->
  <VPage>
    <VTable ref="tableRef" v-bind="tableOptins">
      <template #form="{ form }">
        <XForm
          :fieldList="fieldList"
          :formOption="optionsData"
          :formData="form"
        >
          <template #warehouse="{ input, value }">
            <WarehouseSelect
              @update:model-value="input"
              :model-value="value"
              multiple
              collapse-tags
              collapse-tags-tooltip
              style="width: 240px"
            />
          </template>
          <template #platform="{ input, value }">
            <PlatformSelect
              @update:model-value="input"
              :model-value="value"
              multiple
              collapse-tags
              collapse-tags-tooltip
              style="width: 240px"
            />
          </template>
        </XForm>
      </template>
      <template #toolbar_btns>
        <VActionBar :actions="actions">
          <template #left>
            <span>列表展示:</span>
            <el-checkbox-group v-model="checkList" @change="handleCheck">
              <el-checkbox label="上网时效" value="online" />
              <el-checkbox label="出库时效" value="ostock" />
              <el-checkbox label="签收时效" value="finish" />
              <el-checkbox label="推单-上网" value="push" />
            </el-checkbox-group>
          </template>
        </VActionBar>
      </template>
    </VTable>
    <ChannelDetail ref="channelDetailRef" :option="optionsData" />
    <OrdersDetail ref="ordersDetailRef" />
  </VPage>
</template>
<script setup lang="jsx">
import { ElMessage } from "element-plus";
import XForm from "@/components/XForm/index.vue";
import WarehouseSelect from "@/components/WarehouseSelect/index.vue";
import { logisticsTimelinessList, logisticsTimelinessUpdate } from "./api.js";
import PlatformSelect from "@/components/PlatformSelect/index.vue";
import ChannelDetail from "./components/channelDetail/index";
import OrdersDetail from "./components/ordersDetail/index";
import { useEnumSelect } from "@/hooks/useEnumSelect.js";
import { tipMessage, getTableSelect } from "@/hooks/tool.js";
import { logisticsManageAuthList } from "@/Auth/logistics-manage.js";
const { proxy } = getCurrentInstance();
const { options: optionsData } = useEnumSelect({
  params: ["order_indicator_type"],
});
const channelDetailVisible = ref(false);
const channelDetailRef = ref();
const handleWarehouse = (row) => {
  const {
    order_day,
    ship_warehouse_id,
    platform_id,
    ship_warehouse_name,
    platform_name,
  } = row;
  let obj = {
    order_day,
    ship_warehouse_id,
    platform_id,
    ship_warehouse_name,
    platform_name,
  };
  channelDetailRef.value.open(obj);
};
const ordersDetailRef = ref();
const handlePackage = (name, row) => {
  let indicator_type = optionsData.value.order_indicator_type.find(
    (item) => item.label == name
  )?.value;
  if (!indicator_type) return;
  const {
    order_day,
    ship_warehouse_id,
    platform_id,
    ship_warehouse_name,
    platform_name,
  } = row;
  let obj = {
    order_day,
    ship_warehouse_id,
    platform_id,
    indicator_type,
    ship_warehouse_name,
    platform_name,
  };
  ordersDetailRef.value.open(obj);
};
let lastCallTime = 0;
const handleBatchUpdate = async () => {
  const now = Date.now();
  if (now - lastCallTime < 10000) {
    return ElMessage.warning("操作过于频繁，请稍后再试！");
  }
  lastCallTime = now;
  try {
    const { code, message, data } = await logisticsTimelinessUpdate();
    if (code !== 200) return ElMessage.error(message);
    ElMessage.success(data.result_msg);
    refresh();
  } catch (error) {
    console.error("更新失败", error);
  }

  // const rows = getTableSelect(tableRef.value);
  // if (!rows.length) return ElMessage.warning("请至少选择一条数据!");
  // proxy?.confirm({
  //   title: "提示",
  //   content: (
  //     <p>
  //       确认更新选中的
  //       <span class="text-[#3366F1] px-[4px]">{id.length || 1}</span>
  //       条数据？
  //     </p>
  //   ),
  //   beforeClose: async (action, instance, done) => {
  //     if (action === "confirm") {
  //       try {
  //         instance.confirmButtonLoading = true;
  //         instance.confirmButtonText = "处理中..";
  //         const { code, message, data } = await logisticsTimelinessUpdate({
  //           id,
  //           status: flag ? 1 : 0,
  //         });
  //         if (code != 200) {
  //           ElMessage.error(message);
  //           return;
  //         }
  //         tipMessage(data);
  //       } catch (e) {
  //         console.error(e);
  //       } finally {
  //         instance.confirmButtonLoading = false;
  //         done();
  //         refresh();
  //       }
  //     } else {
  //       done();
  //     }
  //   },
  // });
};
const refresh = () => {
  tableRef?.value.query()
};

const getOrderBy = (sorts = []) => {
  const order_by = {};
  sorts.forEach(({ field, order }) => {
    if (order) {
      let mappedItem = optionsData.value?.order_indicator_type?.find(
        (item) => item.label == field
      )?.value;
      let backendField = mappedItem || field;
      order_by[backendField] = order.toUpperCase();
    }
  });
  return order_by;
};

const hanleClearSort = () => {
  tableRef.value.$table.clearSort();
  refresh();
};

const tableRef = ref();
const tableOptins = reactive({
  formConfig: {
    data: {
      dateRange: { type: "order_day", value: [], range: false }, // 组合+日期范围
    },
  },
  tooltipConfig: {
    showAll: false,
  },
  // sortConfig: {
  //   multiple: true,
  //   remote: true,
  // },
  border: true,
  isToggle: true,
  columns: [
    { fixed: "left", width: 50, type: "checkbox" },
    {
      title: "下单日期",
      width: 100,
      field: "order_day",
      fixed: "left",
      sortable: true,
    },

    {
      title: "发货仓库",
      width: 160,
      field: "ship_warehouse_name",
      fixed: "left",
      slots: {
        default: ({ row }) => (
          <el-link type="primary" onClick={() => handleWarehouse(row)}>
            {row.ship_warehouse_name}
          </el-link>
        ),
      },
    },
    { title: "平台", width: 120, field: "platform_name", fixed: "left" },
    {
      title: "包裹总数",
      width: 120,
      field: "total_package_cnt",
      sortable: true,
      slots: {
        default: ({ row }) => {
          if (row.total_package_cnt) {
            return (
              <el-link
                type="primary"
                onClick={() => handlePackage("total_package_cnt", row)}
              >
                {row.total_package_cnt}
              </el-link>
            );
          } else {
            return <div>{row.total_package_cnt}</div>;
          }
        },
      },
    },
    {
      title: "总重量(KG)",
      width: 160,
      field: "total_weight_kg",
      sortable: true,
    },
    {
      title: "总体积(CBM)",
      width: 160,
      field: "total_volume_cbm",
      sortable: true,
    },
    {
      title: "总运费(CNY)",
      width: 160,
      field: "total_freight_cny",
      sortable: true,
    },
    {
      title: "24小时内上网",
      align: "center",
      children: [
        {
          field: "online_1di_package_cnt",
          title: "包裹",
          width: 100,
          visible: true,
          sortable: true,
          slots: {
            default: ({ row }) => {
              if (row.online_1di_package_cnt) {
                return (
                  <el-link
                    type="primary"
                    onClick={() => handlePackage("online_1di_package_cnt", row)}
                  >
                    {row.online_1di_package_cnt}
                  </el-link>
                );
              } else {
                return <div>{row.online_1di_package_cnt}</div>;
              }
            },
          },
        },
        {
          field: "online_1di_package_cnt_rate",
          visible: true,
          title: "占比",
          width: 100,
        },
      ],
    },
    {
      title: "48小时内上网",
      align: "center",
      children: [
        {
          field: "online_2di_package_cnt",
          title: "包裹",
          width: 100,
          visible: true,
          sortable: true,
          slots: {
            default: ({ row }) => {
              if (row.online_2di_package_cnt) {
                return (
                  <el-link
                    type="primary"
                    onClick={() => handlePackage("online_2di_package_cnt", row)}
                  >
                    {row.online_2di_package_cnt}
                  </el-link>
                );
              } else {
                return <div>{row.online_2di_package_cnt}</div>;
              }
            },
          },
        },
        {
          field: "online_2di_package_cnt_rate",
          visible: true,
          title: "占比",
          width: 100,
        },
      ],
    },
    {
      title: "72小时内上网",
      align: "center",
      children: [
        {
          field: "online_3di_package_cnt",
          title: "包裹",
          width: 100,
          sortable: true,
          visible: true,
          slots: {
            default: ({ row }) => {
              if (row.online_3di_package_cnt) {
                return (
                  <el-link
                    type="primary"
                    onClick={() => handlePackage("online_3di_package_cnt", row)}
                  >
                    {row.online_3di_package_cnt}
                  </el-link>
                );
              } else {
                return <div>{row.online_3di_package_cnt}</div>;
              }
            },
          },
        },
        {
          field: "online_3di_package_cnt_rate",
          visible: true,
          title: "占比",
          width: 100,
        },
      ],
    },
    {
      title: "96小时内上网",
      align: "center",
      children: [
        {
          field: "online_4di_package_cnt",
          title: "包裹",
          width: 100,
          sortable: true,
          visible: true,
          slots: {
            default: ({ row }) => {
              if (row.online_4di_package_cnt) {
                return (
                  <el-link
                    type="primary"
                    onClick={() => handlePackage("online_4di_package_cnt", row)}
                  >
                    {row.online_4di_package_cnt}
                  </el-link>
                );
              } else {
                return <div>{row.online_4di_package_cnt}</div>;
              }
            },
          },
        },
        {
          field: "online_4di_package_cnt_rate",
          visible: true,
          title: "占比",
          width: 100,
        },
      ],
    },
    {
      title: "超期上网",
      align: "center",
      children: [
        {
          field: "online_overdue_package_cnt",
          title: "包裹",
          width: 100,
          sortable: true,
          visible: true,
          slots: {
            default: ({ row }) => {
              if (row.online_overdue_package_cnt) {
                return (
                  <el-link
                    type="primary"
                    onClick={() =>
                      handlePackage("online_overdue_package_cnt", row)
                    }
                  >
                    {row.online_overdue_package_cnt}
                  </el-link>
                );
              } else {
                return <div>{row.online_overdue_package_cnt}</div>;
              }
            },
          },
        },
        {
          field: "online_overdue_package_cnt_rate",
          visible: true,
          title: "占比",
          width: 100,
        },
      ],
    },
    {
      title: "未上网",
      align: "center",
      children: [
        {
          field: "online_waiting_package_cnt",
          title: "包裹",
          width: 100,
          sortable: true,
          visible: true,
          slots: {
            default: ({ row }) => {
              if (row.online_waiting_package_cnt) {
                return (
                  <el-link
                    type="primary"
                    onClick={() =>
                      handlePackage("online_waiting_package_cnt", row)
                    }
                  >
                    {row.online_waiting_package_cnt}
                  </el-link>
                );
              } else {
                return <div>{row.online_waiting_package_cnt}</div>;
              }
            },
          },
        },
        {
          field: "online_waiting_package_cnt_rate",
          visible: true,
          title: "占比",
          width: 100,
        },
      ],
    },
    {
      title: "24小时内出库",
      align: "center",
      children: [
        {
          field: "ostock_1di_package_cnt",
          title: "包裹",
          width: 100,
          sortable: true,
          visible: true,
          slots: {
            default: ({ row }) => {
              if (row.ostock_1di_package_cnt) {
                return (
                  <el-link
                    type="primary"
                    onClick={() => handlePackage("ostock_1di_package_cnt", row)}
                  >
                    {row.ostock_1di_package_cnt}
                  </el-link>
                );
              } else {
                return <div>{row.ostock_1di_package_cnt}</div>;
              }
            },
          },
        },
        {
          field: "ostock_1di_package_cnt_rate",
          visible: true,
          title: "占比",
          width: 100,
        },
      ],
    },
    {
      title: "48小时内出库",
      align: "center",
      children: [
        {
          field: "ostock_2di_package_cnt",
          title: "包裹",
          width: 100,
          visible: true,
          sortable: true,
          slots: {
            default: ({ row }) => {
              if (row.ostock_2di_package_cnt) {
                return (
                  <el-link
                    type="primary"
                    onClick={() => handlePackage("ostock_2di_package_cnt", row)}
                  >
                    {row.ostock_2di_package_cnt}
                  </el-link>
                );
              } else {
                return <div>{row.ostock_2di_package_cnt}</div>;
              }
            },
          },
        },
        {
          field: "ostock_2di_package_cnt_rate",
          visible: true,
          title: "占比",
          width: 100,
        },
      ],
    },
    {
      title: "72小时内出库",
      align: "center",
      children: [
        {
          field: "ostock_3di_package_cnt",
          title: "包裹",
          width: 100,
          sortable: true,
          visible: true,
          slots: {
            default: ({ row }) => {
              if (row.ostock_3di_package_cnt) {
                return (
                  <el-link
                    type="primary"
                    onClick={() => handlePackage("ostock_3di_package_cnt", row)}
                  >
                    {row.ostock_3di_package_cnt}
                  </el-link>
                );
              } else {
                return <div>{row.ostock_3di_package_cnt}</div>;
              }
            },
          },
        },
        {
          field: "ostock_3di_package_cnt_rate",
          visible: true,
          title: "占比",
          width: 100,
        },
      ],
    },
    {
      title: "96小时内出库",
      align: "center",
      children: [
        {
          field: "ostock_4di_package_cnt",
          title: "包裹",
          width: 100,
          sortable: true,
          visible: true,
          slots: {
            default: ({ row }) => {
              if (row.ostock_4di_package_cnt) {
                return (
                  <el-link
                    type="primary"
                    onClick={() => handlePackage("ostock_4di_package_cnt", row)}
                  >
                    {row.ostock_4di_package_cnt}
                  </el-link>
                );
              } else {
                return <div>{row.ostock_4di_package_cnt}</div>;
              }
            },
          },
        },
        {
          field: "ostock_4di_package_cnt_rate",
          visible: true,
          title: "占比",
          width: 100,
        },
      ],
    },
    {
      title: "超期出库",
      align: "center",
      children: [
        {
          field: "ostock_overdue_package_cnt",
          title: "包裹",
          width: 100,
          visible: true,
          sortable: true,
          slots: {
            default: ({ row }) => {
              if (row.ostock_overdue_package_cnt) {
                return (
                  <el-link
                    type="primary"
                    onClick={() =>
                      handlePackage("ostock_overdue_package_cnt", row)
                    }
                  >
                    {row.ostock_overdue_package_cnt}
                  </el-link>
                );
              } else {
                return <div>{row.ostock_overdue_package_cnt}</div>;
              }
            },
          },
        },
        {
          field: "ostock_overdue_package_cnt_rate",
          visible: true,
          title: "占比",
          width: 100,
        },
      ],
    },
    {
      title: "未出库",
      align: "center",
      children: [
        {
          field: "ostock_waiting_package_cnt",
          title: "包裹",
          width: 100,
          sortable: true,
          visible: true,
          slots: {
            default: ({ row }) => {
              if (row.ostock_waiting_package_cnt) {
                return (
                  <el-link
                    type="primary"
                    onClick={() =>
                      handlePackage("ostock_waiting_package_cnt", row)
                    }
                  >
                    {row.ostock_waiting_package_cnt}
                  </el-link>
                );
              } else {
                return <div>{row.ostock_waiting_package_cnt}</div>;
              }
            },
          },
        },
        {
          field: "ostock_waiting_package_cnt_rate",
          visible: true,
          title: "占比",
          width: 100,
        },
      ],
    },
    {
      title: "24小时内签收",
      align: "center",
      children: [
        {
          field: "finish_1di_package_cnt",
          title: "包裹",
          width: 100,
          sortable: true,
          visible: true,
          slots: {
            default: ({ row }) => {
              if (row.finish_1di_package_cnt) {
                return (
                  <el-link
                    type="primary"
                    onClick={() => handlePackage("finish_1di_package_cnt", row)}
                  >
                    {row.finish_1di_package_cnt}
                  </el-link>
                );
              } else {
                return <div>{row.finish_1di_package_cnt}</div>;
              }
            },
          },
        },
        {
          field: "finish_1di_package_cnt_rate",
          visible: true,
          title: "占比",
          width: 100,
        },
      ],
    },
    {
      title: "48小时内签收",
      align: "center",
      children: [
        {
          field: "finish_2di_package_cnt",
          title: "包裹",
          width: 100,
          sortable: true,
          visible: true,
          slots: {
            default: ({ row }) => {
              if (row.finish_2di_package_cnt) {
                return (
                  <el-link
                    type="primary"
                    onClick={() => handlePackage("finish_2di_package_cnt", row)}
                  >
                    {row.finish_2di_package_cnt}
                  </el-link>
                );
              } else {
                return <div>{row.finish_2di_package_cnt}</div>;
              }
            },
          },
        },
        {
          field: "finish_2di_package_cnt_rate",
          visible: true,
          title: "占比",
          width: 100,
        },
      ],
    },
    {
      title: "72小时内签收",
      align: "center",
      children: [
        {
          field: "finish_3di_package_cnt",
          title: "包裹",
          width: 100,
          visible: true,
          sortable: true,
          slots: {
            default: ({ row }) => {
              if (row.finish_3di_package_cnt) {
                return (
                  <el-link
                    type="primary"
                    onClick={() => handlePackage("finish_3di_package_cnt", row)}
                  >
                    {row.finish_3di_package_cnt}
                  </el-link>
                );
              } else {
                return <div>{row.finish_3di_package_cnt}</div>;
              }
            },
          },
        },
        {
          field: "finish_3di_package_cnt_rate",
          visible: true,
          title: "占比",
          width: 100,
        },
      ],
    },
    {
      title: "96小时内签收",
      align: "center",
      children: [
        {
          field: "finish_4di_package_cnt",
          title: "包裹",
          width: 100,
          sortable: true,
          visible: true,
          slots: {
            default: ({ row }) => {
              if (row.finish_4di_package_cnt) {
                return (
                  <el-link
                    type="primary"
                    onClick={() => handlePackage("finish_4di_package_cnt", row)}
                  >
                    {row.finish_4di_package_cnt}
                  </el-link>
                );
              } else {
                return <div>{row.finish_4di_package_cnt}</div>;
              }
            },
          },
        },
        {
          field: "finish_4di_package_cnt_rate",
          visible: true,
          title: "占比",
          width: 100,
        },
      ],
    },
    {
      title: "超期签收",
      align: "center",
      children: [
        {
          field: "finish_overdue_package_cnt",
          title: "包裹",
          sortable: true,
          width: 100,
          visible: true,
          slots: {
            default: ({ row }) => {
              if (row.finish_overdue_package_cnt) {
                return (
                  <el-link
                    type="primary"
                    onClick={() =>
                      handlePackage("finish_overdue_package_cnt", row)
                    }
                  >
                    {row.finish_overdue_package_cnt}
                  </el-link>
                );
              } else {
                return <div>{row.finish_overdue_package_cnt}</div>;
              }
            },
          },
        },
        {
          field: "finish_overdue_package_cnt_rate",
          visible: true,
          title: "占比",
          width: 100,
        },
      ],
    },
    {
      title: "未签收",
      align: "center",
      children: [
        {
          field: "finish_waiting_package_cnt",
          title: "包裹",
          sortable: true,
          visible: true,
          width: 100,
          slots: {
            default: ({ row }) => {
              if (row.finish_waiting_package_cnt) {
                return (
                  <el-link
                    type="primary"
                    onClick={() =>
                      handlePackage("finish_waiting_package_cnt", row)
                    }
                  >
                    {row.finish_waiting_package_cnt}
                  </el-link>
                );
              } else {
                return <div>{row.finish_waiting_package_cnt}</div>;
              }
            },
          },
        },
        {
          field: "finish_waiting_package_cnt_rate",
          visible: true,
          title: "占比",
          width: 100,
        },
      ],
    },
    {
      title: "24小时内推单-上网",
      align: "center",
      children: [
        {
          field: "push_online_1di_package_cnt",
          title: "包裹",
          sortable: true,
          visible: true,
          width: 100,
          slots: {
            default: ({ row }) => {
              if (row.push_online_1di_package_cnt) {
                return (
                  <el-link
                    type="primary"
                    onClick={() =>
                      handlePackage("push_online_1di_package_cnt", row)
                    }
                  >
                    {row.push_online_1di_package_cnt}
                  </el-link>
                );
              } else {
                return <div>{row.push_online_1di_package_cnt}</div>;
              }
            },
          },
        },
        {
          field: "push_online_1di_package_cnt_rate",
          title: "占比",
          visible: true,
          width: 100,
        },
      ],
    },
    {
      title: "48小时内推单-上网",
      align: "center",
      children: [
        {
          field: "push_online_2di_package_cnt",
          title: "包裹",
          sortable: true,
          visible: true,
          width: 100,
          slots: {
            default: ({ row }) => {
              if (row.push_online_2di_package_cnt) {
                return (
                  <el-link
                    type="primary"
                    onClick={() =>
                      handlePackage("push_online_2di_package_cnt", row)
                    }
                  >
                    {row.push_online_2di_package_cnt}
                  </el-link>
                );
              } else {
                return <div>{row.push_online_2di_package_cnt}</div>;
              }
            },
          },
        },
        {
          field: "push_online_2di_package_cnt_rate",
          title: "占比",
          visible: true,
          width: 100,
        },
      ],
    },
    {
      title: "72小时内推单-上网",
      align: "center",
      children: [
        {
          field: "push_online_3di_package_cnt",
          title: "包裹",
          sortable: true,
          visible: true,
          width: 100,
          slots: {
            default: ({ row }) => {
              if (row.push_online_3di_package_cnt) {
                return (
                  <el-link
                    type="primary"
                    onClick={() =>
                      handlePackage("push_online_3di_package_cnt", row)
                    }
                  >
                    {row.push_online_3di_package_cnt}
                  </el-link>
                );
              } else {
                return <div>{row.push_online_3di_package_cnt}</div>;
              }
            },
          },
        },
        {
          field: "push_online_3di_package_cnt_rate",
          title: "占比",
          visible: true,
          width: 100,
        },
      ],
    },
    {
      title: "96小时内推单-上网",
      align: "center",
      children: [
        {
          field: "push_online_4di_package_cnt",
          title: "包裹",
          sortable: true,
          visible: true,
          width: 100,
          slots: {
            default: ({ row }) => {
              if (row.push_online_4di_package_cnt) {
                return (
                  <el-link
                    type="primary"
                    onClick={() =>
                      handlePackage("push_online_4di_package_cnt", row)
                    }
                  >
                    {row.push_online_4di_package_cnt}
                  </el-link>
                );
              } else {
                return <div>{row.push_online_4di_package_cnt}</div>;
              }
            },
          },
        },
        {
          field: "push_online_4di_package_cnt_rate",
          title: "占比",
          width: 100,
          visible: true,
        },
      ],
    },
    {
      title: "超期推单-上网",
      align: "center",
      children: [
        {
          field: "push_online_overdue_package_cnt",
          title: "包裹",
          sortable: true,
          visible: true,
          width: 100,
          slots: {
            default: ({ row }) => {
              if (row.push_online_overdue_package_cnt) {
                return (
                  <el-link
                    type="primary"
                    onClick={() =>
                      handlePackage("push_online_overdue_package_cnt", row)
                    }
                  >
                    {row.push_online_overdue_package_cnt}
                  </el-link>
                );
              } else {
                return <div>{row.push_online_overdue_package_cnt}</div>;
              }
            },
          },
        },
        {
          field: "push_online_overdue_package_cnt_rate",
          title: "占比",
          visible: true,
          width: 100,
        },
      ],
    },
  ],
  proxyConfig: {
    props: {
      sort: "order_by",
      sortField: "field",
      sortOrder: "order",
    },
    autoLoad: false, // 是否自动加载查询数据
    props: {
      result: "data",
      total: "total",
    },
    ajax: {
      query: ({ page, form, sorts }) => {
        const order_by = getOrderBy(sorts);
        return logisticsTimelinessList({ ...form, ...page, order_by });
      },
    },
  },
});
const allColumns = ref([]);
onMounted(() => {
  nextTick(() => {
    allColumns.value = tableRef.value.$table.getColumns();
  });
});
const checkList = ref(["online", "ostock", "finish", "push"]);
const handleCheck = async (fields) => {
  tableRef.value.$table.refreshColumn();
  let obj = {
    online: [
      "online_1di_package_cnt",
      "online_1di_package_cnt_rate",
      "online_2di_package_cnt",
      "online_2di_package_cnt_rate",
      "online_3di_package_cnt",
      "online_3di_package_cnt_rate",
      "online_4di_package_cnt",
      "online_4di_package_cnt_rate",
      "online_overdue_package_cnt",
      "online_overdue_package_cnt_rate",
      "online_waiting_package_cnt",
      "online_waiting_package_cnt_rate",
    ],
    ostock: [
      "ostock_1di_package_cnt",
      "ostock_1di_package_cnt_rate",
      "ostock_2di_package_cnt",
      "ostock_2di_package_cnt_rate",
      "ostock_3di_package_cnt",
      "ostock_3di_package_cnt_rate",
      "ostock_4di_package_cnt",
      "ostock_4di_package_cnt_rate",
      "ostock_overdue_package_cnt",
      "ostock_overdue_package_cnt_rate",
      "ostock_waiting_package_cnt",
      "ostock_waiting_package_cnt_rate",
    ],
    finish: [
      "finish_1di_package_cnt",
      "finish_1di_package_cnt_rate",
      "finish_2di_package_cnt",
      "finish_2di_package_cnt_rate",
      "finish_3di_package_cnt",
      "finish_3di_package_cnt_rate",
      "finish_4di_package_cnt",
      "finish_4di_package_cnt_rate",
      "finish_overdue_package_cnt",
      "finish_overdue_package_cnt_rate",
      "finish_waiting_package_cnt",
      "finish_waiting_package_cnt_rate",
    ],
    push: [
      "push_online_1di_package_cnt",
      "push_online_1di_package_cnt_rate",
      "push_online_2di_package_cnt",
      "push_online_2di_package_cnt_rate",
      "push_online_3di_package_cnt",
      "push_online_3di_package_cnt_rate",
      "push_online_4di_package_cnt",
      "push_online_4di_package_cnt_rate",
      "push_online_overdue_package_cnt",
      "push_online_overdue_package_cnt_rate",
    ],
  };

  const targetFields = fields.flatMap((key) => obj[key] || []);
  const uniqueFields = [...new Set(targetFields)];

  const restGroupKeys = Object.keys(obj).filter((key) => !fields.includes(key));
  const restFields = restGroupKeys.flatMap((key) => obj[key] || []);
  const uniqueRestFields = [...new Set(restFields)];

  uniqueFields?.forEach((field) => {
    const matchedCol = allColumns.value.find((col) => col.field === field);
    if (matchedCol) {
      matchedCol.visible = true;
    }
  });
  uniqueRestFields?.forEach((field) => {
    const matchedCol = allColumns.value.find((col) => col.field === field);
    if (matchedCol) {
      matchedCol.visible = false;
    }
  });
  await nextTick(); // 等待 DOM 更新
  tableRef.value.$table.refreshColumn();
};
const fieldList = [
  {
    field: "dateRange",
    type: "dateTimeRange",
    style: { width: "360px" },
    select: [{ label: "下单时间", value: "order_day" }],
    selectStyle: { width: "100px" },
    itemProps: {
      startPlaceholder: "最早时间",
      endPlaceholder: "最晚时间",
    },
  },
  {
    field: "ship_warehouse_id",
    label: "发货仓库",
    slot: "warehouse",
  },
  {
    field: "platform_id",
    label: "平台",
    slot: "platform",
  },
];
const actions = [
  {
    label: "更新数据",
    type: "primary",
    fn: handleBatchUpdate,
    permission: [logisticsManageAuthList.last_mile_timeliness.update],
  },
  {
    label: "清空排序",
    fn: hanleClearSort,
  },
];
</script>
<style lang="scss" scoped></style>
